aboutsummaryrefslogtreecommitdiff
path: root/src/routes/projects/[projectId]/routine/[routineId]/+page.server.ts
blob: f2b6551f174b5d9bf68a9530182e9cad30a1a2c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

import { error, fail } from '@sveltejs/kit';
import { getById } from '$lib/server/services/projects';
import {
	edit,
	complete,
	loadCurrent,
	loadCompletions,
} from '$lib/server/services/routines';
import { numericId, parseFormString } from '$lib/server/services/validation';
import {
	parseIntervalToDays,
	computeRoutineState,
} from '$lib/shared/routine-metrics';
import type { Actions, PageServerLoad } from './$types';

function parseFrequencyDays(value: FormDataEntryValue | null): number {
	const days = parseIntervalToDays(value?.toString() ?? '');
	if (days === null) {
		throw new Error('Invalid interval');
	}
	return days;
}

export const load: PageServerLoad = async ({ params }) => {
	const projectId = numericId(params.projectId);
	const routineId = numericId(params.routineId);

	const project = await getById(projectId);
	if (!project || project.archived) {
		throw error(404, 'Projet introuvable');
	}

	const routine = await loadCurrent(routineId);
	if (!routine || routine.projectId !== projectId) {
		throw error(404, 'Routine introuvable');
	}

	const completions = await loadCompletions(routineId);
	// An archived routine is frozen at its archive instant: state and chart
	// stop there instead of now.
	const at = routine.archivedAt ? Date.parse(routine.archivedAt) : Date.now();
	return {
		project: { id: project.id, name: project.name },
		routine,
		completions,
		state: computeRoutineState(routine, completions, at),
	};
};

export const actions: Actions = {
	edit: async ({ request, params }) => {
		const current = await loadCurrent(numericId(params.routineId));
		if (!current) {
			return fail(400, { error: 'Routine introuvable' });
		}
		if (current.archived) {
			return fail(400, { error: 'Routine archivée' });
		}
		const data = await request.formData();
		const name = parseFormString(data.get('name'));
		if (!name) {
			return fail(400, { error: 'Le nom est requis' });
		}
		const description = data.get('description')?.toString() ?? '';
		try {
			await edit(numericId(params.routineId), {
				name,
				frequencyDays: parseFrequencyDays(data.get('frequencyDays')),
				description,
			});
		} catch {
			return fail(400, { error: 'Fréquence invalide' });
		}
		return { success: true, type: 'edit' as const };
	},

	complete: async ({ request, params }) => {
		const current = await loadCurrent(numericId(params.routineId));
		if (!current) {
			return fail(400, { error: 'Routine introuvable' });
		}
		if (current.archived) {
			return fail(400, { error: 'Routine archivée' });
		}
		const data = await request.formData();
		try {
			await complete(
				numericId(params.routineId),
				parseFormString(data.get('note')),
				new Date().toISOString(),
			);
		} catch {
			return fail(400, { error: 'Routine introuvable' });
		}
		return { success: true };
	},
};